﻿using System;
using Engine;
using Game;
namespace Mekiasm
{
    public class MekModelGenerator
    {
        public struct CornerLights
        {
            public float L000;

            public float L001;

            public float L010;

            public float L011;

            public float L100;

            public float L101;

            public float L110;

            public float L111;
        }

        public static Vector2[] m_textureCoordinates = new Vector2[8]
        {
            new Vector2(0.001f, 0.999f),
            new Vector2(0.999f, 0.999f),
            new Vector2(0.999f, 0.001f),
            new Vector2(0.001f, 0.001f),
            new Vector2(0.001f, 0.999f),
            new Vector2(0.999f, 0.999f),
            new Vector2(0.999f, 0.001f),
            new Vector2(0.001f, 0.001f)
        };

        public readonly Terrain Terrain;

        public readonly SubsystemTerrain SubsystemTerrain;

        public Point3 m_cornerLightsPosition;

        public CornerLights[] m_cornerLightsByFace = new CornerLights[6];

        public bool[] m_visibleSides = new bool[6];

        public static bool EnableLight = true;

        public MekModelGenerator()
        {
            SubsystemTerrain = ILibrary.SubsystemTerrain;
            Terrain = ILibrary.SubsystemTerrain.Terrain;
            ResetCache();
        }

        public void ResetCache()
        {
            m_cornerLightsPosition = new Point3(int.MaxValue);
        }

        public static void SetupCornerVertex(float x, float y, float z, Color color, int light, int face, int corner, ref TerrainVertex vertex)
        {
            float num = LightingManager.LightIntensityByLightValueAndFace[light + 16 * face];
            if (EnableLight)
            {
                color = new Color((byte)((float)(int)color.R * num), (byte)((float)(int)color.G * num), (byte)((float)(int)color.B * num), color.A);
            }
            float tx = m_textureCoordinates[corner].X;
            float ty = m_textureCoordinates[corner].Y;
            SetupVertex(x, y, z, color, tx, ty, ref vertex);
        }
        public static void SetupCornerVertex(float x, float y, float z, Color color, int light, int face, int textureSlot, int corner, ref TerrainVertex vertex)
        {
            float num = LightingManager.LightIntensityByLightValueAndFace[light + 16 * face];
            if (EnableLight)
            {
                color = new Color((byte)((float)(int)color.R * num), (byte)((float)(int)color.G * num), (byte)((float)(int)color.B * num), color.A);
            }
            float tx = (m_textureCoordinates[corner].X + (float)(textureSlot % 16)) / 16f;
            float ty = (m_textureCoordinates[corner].Y + (float)(textureSlot / 16)) / 16f;
            SetupVertex(x, y, z, color, tx, ty, ref vertex);
        }

        public static void SetupLitCornerVertex(float x, float y, float z, Color color, int corner, ref TerrainVertex vertex)
        {
            float tx = m_textureCoordinates[corner].X;
            float ty = m_textureCoordinates[corner].Y;
            SetupVertex(x, y, z, color, tx, ty, ref vertex);
        }
        public static void GenerateSaplingModel(Block block, int value, int x, int y, int z, Color color, TerrainGeometrySubset subset)
        {
            DynamicArray<TerrainVertex> vertices = subset.Vertices;
            var indices = subset.Indices;
            int num = Terrain.ExtractLight(value);
            float num2 = LightingManager.LightIntensityByLightValueAndFace[num + 64];
            Color color2 = new Color((byte)((float)(int)color.R * num2), (byte)((float)(int)color.G * num2), (byte)((float)(int)color.B * num2), color.A);
            int count = vertices.Count;
            vertices.Count += 8;
            if ((x & 1) == 0)
            {
                SetupLitCornerVertex(x, y, z, color2, 0, ref vertices.Array[count]);
                SetupLitCornerVertex(x + 1, y, z + 1, color2, 1, ref vertices.Array[count + 1]);
                SetupLitCornerVertex(x + 1, y + 1, z + 1, color2, 2, ref vertices.Array[count + 2]);
                SetupLitCornerVertex(x, y + 1, z, color2, 3, ref vertices.Array[count + 3]);
            }
            else
            {
                SetupLitCornerVertex(x, y, z, color2, 1, ref vertices.Array[count]);
                SetupLitCornerVertex(x + 1, y, z + 1, color2, 0, ref vertices.Array[count + 1]);
                SetupLitCornerVertex(x + 1, y + 1, z + 1, color2, 3, ref vertices.Array[count + 2]);
                SetupLitCornerVertex(x, y + 1, z, color2, 2, ref vertices.Array[count + 3]);
            }

            if ((z & 1) == 0)
            {
                SetupLitCornerVertex(x, y, z + 1, color2, 0, ref vertices.Array[count + 4]);
                SetupLitCornerVertex(x + 1, y, z, color2, 1, ref vertices.Array[count + 5]);
                SetupLitCornerVertex(x + 1, y + 1, z, color2, 2, ref vertices.Array[count + 6]);
                SetupLitCornerVertex(x, y + 1, z + 1, color2, 3, ref vertices.Array[count + 7]);
            }
            else
            {
                SetupLitCornerVertex(x, y, z + 1, color2, 1, ref vertices.Array[count + 4]);
                SetupLitCornerVertex(x + 1, y, z, color2, 0, ref vertices.Array[count + 5]);
                SetupLitCornerVertex(x + 1, y + 1, z, color2, 3, ref vertices.Array[count + 6]);
                SetupLitCornerVertex(x, y + 1, z + 1, color2, 2, ref vertices.Array[count + 7]);
            }

            int count2 = indices.Count;
            indices.Count += 24;
            indices.Array[count2] = count;
            indices.Array[count2 + 1] = (count + 1);
            indices.Array[count2 + 2] = (count + 2);
            indices.Array[count2 + 3] = (count + 2);
            indices.Array[count2 + 4] = (count + 1);
            indices.Array[count2 + 5] = count;
            indices.Array[count2 + 6] = (count + 2);
            indices.Array[count2 + 7] = (count + 3);
            indices.Array[count2 + 8] = count;
            indices.Array[count2 + 9] = count;
            indices.Array[count2 + 10] = (count + 3);
            indices.Array[count2 + 11] = (count + 2);
            indices.Array[count2 + 12] = (count + 4);
            indices.Array[count2 + 13] = (count + 5);
            indices.Array[count2 + 14] = (count + 6);
            indices.Array[count2 + 15] = (count + 6);
            indices.Array[count2 + 16] = (count + 5);
            indices.Array[count2 + 17] = (count + 4);
            indices.Array[count2 + 18] = (count + 6);
            indices.Array[count2 + 19] = (count + 7);
            indices.Array[count2 + 20] = (count + 4);
            indices.Array[count2 + 21] = (count + 4);
            indices.Array[count2 + 22] = (count + 7);
            indices.Array[count2 + 23] = (count + 6);
        }

        public static void SetupVertex(float x, float y, float z, Color color, float tx, float ty, ref TerrainVertex vertex)
        {
            vertex.X = x;
            vertex.Y = y;
            vertex.Z = z;
            vertex.Tx = (short)(tx * 32767f);
            vertex.Ty = (short)(ty * 32767f);
            vertex.Color = color;
        }
        public void GenerateCubeVertices(Block block, int value, int x, int y, int z, Color color, TerrainGeometrySubset[] subsetsByFace)
        {
            GenerateCubeVertices(block, value, x, y, z, color, subsetsByFace, 0);
            GenerateCubeVertices(block, value, x, y, z, color, subsetsByFace, 1);
            GenerateCubeVertices(block, value, x, y, z, color, subsetsByFace, 2);
            GenerateCubeVertices(block, value, x, y, z, color, subsetsByFace, 3);
            GenerateCubeVertices(block, value, x, y, z, color, subsetsByFace, 4);
            GenerateCubeVertices(block, value, x, y, z, color, subsetsByFace, 5);
        }
        public void GenerateCubeVerticesFace(Block block, int value,int face, int x, int y, int z, Color color, TerrainGeometrySubset[] subsetsByFace)
        {
            GenerateCubeVertices(block, value, x, y, z, color, subsetsByFace, face);
        }
        public void GenerateCubeVerticesBase(Block block, int value, int x, int y, int z, Color color, TerrainGeometrySubset[] subsetsByFace)
        {
            int blockIndex = block.BlockIndex;
            TerrainChunk chunkAtCell = Terrain.GetChunkAtCell(x, z);
            TerrainChunk chunkAtCell2 = Terrain.GetChunkAtCell(x, z + 1);
            TerrainChunk chunkAtCell3 = Terrain.GetChunkAtCell(x + 1, z);
            TerrainChunk chunkAtCell4 = Terrain.GetChunkAtCell(x, z - 1);
            TerrainChunk chunkAtCell5 = Terrain.GetChunkAtCell(x - 1, z);
            int cellValueFast = chunkAtCell2.GetCellValueFast(x & 0xF, y, (z + 1) & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 0, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices = subsetsByFace[0].Vertices;
                var indices = subsetsByFace[0].Indices;
                int faceTextureSlot = block.GetFaceTextureSlot(0, value);
                int count = vertices.Count;
                vertices.Count += 4;
                SetupCubeVertexFace0(x, y, z + 1, 1f, 0, faceTextureSlot, color, ref vertices.Array[count]);
                SetupCubeVertexFace0(x + 1, y, z + 1, 1f, 1, faceTextureSlot, color, ref vertices.Array[count + 1]);
                SetupCubeVertexFace0(x + 1, y + 1, z + 1, 1f, 2, faceTextureSlot, color, ref vertices.Array[count + 2]);
                SetupCubeVertexFace0(x, y + 1, z + 1, 1f, 3, faceTextureSlot, color, ref vertices.Array[count + 3]);
                int count2 = indices.Count;
                indices.Count += 6;
                indices.Array[count2] = count;
                indices.Array[count2 + 1] = (count + 2);
                indices.Array[count2 + 2] = (count + 1);
                indices.Array[count2 + 3] = (count + 2);
                indices.Array[count2 + 4] = count;
                indices.Array[count2 + 5] = (count + 3);
            }

            cellValueFast = chunkAtCell3.GetCellValueFast((x + 1) & 0xF, y, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 1, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices2 = subsetsByFace[1].Vertices;
                var indices2 = subsetsByFace[1].Indices;
                int faceTextureSlot2 = block.GetFaceTextureSlot(1, value);
                int count3 = vertices2.Count;
                vertices2.Count += 4;
                SetupCubeVertexFace1(x + 1, y, z, 1f, 1, faceTextureSlot2, color, ref vertices2.Array[count3]);
                SetupCubeVertexFace1(x + 1, y + 1, z, 1f, 2, faceTextureSlot2, color, ref vertices2.Array[count3 + 1]);
                SetupCubeVertexFace1(x + 1, y + 1, z + 1, 1f, 3, faceTextureSlot2, color, ref vertices2.Array[count3 + 2]);
                SetupCubeVertexFace1(x + 1, y, z + 1, 1f, 0, faceTextureSlot2, color, ref vertices2.Array[count3 + 3]);
                int count4 = indices2.Count;
                indices2.Count += 6;
                indices2.Array[count4] = count3;
                indices2.Array[count4 + 1] = (count3 + 2);
                indices2.Array[count4 + 2] = (count3 + 1);
                indices2.Array[count4 + 3] = (count3 + 2);
                indices2.Array[count4 + 4] = count3;
                indices2.Array[count4 + 5] = (count3 + 3);
            }

            cellValueFast = chunkAtCell4.GetCellValueFast(x & 0xF, y, (z - 1) & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 2, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices3 = subsetsByFace[2].Vertices;
                var indices3 = subsetsByFace[2].Indices;
                int faceTextureSlot3 = block.GetFaceTextureSlot(2, value);
                int count5 = vertices3.Count;
                vertices3.Count += 4;
                SetupCubeVertexFace2(x, y, z, 1f, 1, faceTextureSlot3, color, ref vertices3.Array[count5]);
                SetupCubeVertexFace2(x + 1, y, z, 1f, 0, faceTextureSlot3, color, ref vertices3.Array[count5 + 1]);
                SetupCubeVertexFace2(x + 1, y + 1, z, 1f, 3, faceTextureSlot3, color, ref vertices3.Array[count5 + 2]);
                SetupCubeVertexFace2(x, y + 1, z, 1f, 2, faceTextureSlot3, color, ref vertices3.Array[count5 + 3]);
                int count6 = indices3.Count;
                indices3.Count += 6;
                indices3.Array[count6] = count5;
                indices3.Array[count6 + 1] = (count5 + 1);
                indices3.Array[count6 + 2] = (count5 + 2);
                indices3.Array[count6 + 3] = (count5 + 2);
                indices3.Array[count6 + 4] = (count5 + 3);
                indices3.Array[count6 + 5] = count5;
            }

            cellValueFast = chunkAtCell5.GetCellValueFast((x - 1) & 0xF, y, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 3, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices4 = subsetsByFace[3].Vertices;
                var indices4 = subsetsByFace[3].Indices;
                int faceTextureSlot4 = block.GetFaceTextureSlot(3, value);
                int count7 = vertices4.Count;
                vertices4.Count += 4;
                SetupCubeVertexFace3(x, y, z, 1f, 0, faceTextureSlot4, color, ref vertices4.Array[count7]);
                SetupCubeVertexFace3(x, y + 1, z, 1f, 3, faceTextureSlot4, color, ref vertices4.Array[count7 + 1]);
                SetupCubeVertexFace3(x, y + 1, z + 1, 1f, 2, faceTextureSlot4, color, ref vertices4.Array[count7 + 2]);
                SetupCubeVertexFace3(x, y, z + 1, 1f, 1, faceTextureSlot4, color, ref vertices4.Array[count7 + 3]);
                int count8 = indices4.Count;
                indices4.Count += 6;
                indices4.Array[count8] = count7;
                indices4.Array[count8 + 1] = (count7 + 1);
                indices4.Array[count8 + 2] = (count7 + 2);
                indices4.Array[count8 + 3] = (count7 + 2);
                indices4.Array[count8 + 4] = (count7 + 3);
                indices4.Array[count8 + 5] = count7;
            }

            cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y + 1, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 4, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices5 = subsetsByFace[4].Vertices;
                var indices5 = subsetsByFace[4].Indices;
                int faceTextureSlot5 = block.GetFaceTextureSlot(4, value);
                int count9 = vertices5.Count;
                vertices5.Count += 4;
                SetupCubeVertexFace4(x, y + 1, z, 1f, 3, faceTextureSlot5, color, ref vertices5.Array[count9]);
                SetupCubeVertexFace4(x + 1, y + 1, z, 1f, 2, faceTextureSlot5, color, ref vertices5.Array[count9 + 1]);
                SetupCubeVertexFace4(x + 1, y + 1, z + 1, 1f, 1, faceTextureSlot5, color, ref vertices5.Array[count9 + 2]);
                SetupCubeVertexFace4(x, y + 1, z + 1, 1f, 0, faceTextureSlot5, color, ref vertices5.Array[count9 + 3]);
                int count10 = indices5.Count;
                indices5.Count += 6;
                indices5.Array[count10] = count9;
                indices5.Array[count10 + 1] = (count9 + 1);
                indices5.Array[count10 + 2] = (count9 + 2);
                indices5.Array[count10 + 3] = (count9 + 2);
                indices5.Array[count10 + 4] = (count9 + 3);
                indices5.Array[count10 + 5] = count9;
            }

            cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y - 1, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 5, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices6 = subsetsByFace[5].Vertices;
                var indices6 = subsetsByFace[5].Indices;
                int faceTextureSlot6 = block.GetFaceTextureSlot(5, value);
                int count11 = vertices6.Count;
                vertices6.Count += 4;
                SetupCubeVertexFace5(x, y, z, 1f, 0, faceTextureSlot6, color, ref vertices6.Array[count11]);
                SetupCubeVertexFace5(x + 1, y, z, 1f, 1, faceTextureSlot6, color, ref vertices6.Array[count11 + 1]);
                SetupCubeVertexFace5(x + 1, y, z + 1, 1f, 2, faceTextureSlot6, color, ref vertices6.Array[count11 + 2]);
                SetupCubeVertexFace5(x, y, z + 1, 1f, 3, faceTextureSlot6, color, ref vertices6.Array[count11 + 3]);
                int count12 = indices6.Count;
                indices6.Count += 6;
                indices6.Array[count12] = count11;
                indices6.Array[count12 + 1] = (count11 + 2);
                indices6.Array[count12 + 2] = (count11 + 1);
                indices6.Array[count12 + 3] = (count11 + 2);
                indices6.Array[count12 + 4] = count11;
                indices6.Array[count12 + 5] = (count11 + 3);
            }
        }
        public void GenerateCubeVertices(Block block, int value, int x, int y, int z, Color color, TerrainGeometrySubset[] subsetsByFace, int face)
        {
            int blockIndex = block.BlockIndex;
            TerrainChunk chunkAtCell = null;
            TerrainChunk chunkAtCell2 = null;
            TerrainChunk chunkAtCell3 = null;
            TerrainChunk chunkAtCell4 = null;
            TerrainChunk chunkAtCell5 = null;


            switch (face)
            {
                case 0:
                    chunkAtCell2 = Terrain.GetChunkAtCell(x, z + 1);
                    int cellValueFast = chunkAtCell2.GetCellValueFast(x & 0xF, y, (z + 1) & 0xF);
                    if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, face, value, cellValueFast))
                    {
                        DynamicArray<TerrainVertex> vertices = subsetsByFace[0].Vertices;
                        var indices = subsetsByFace[0].Indices;
                        int count = vertices.Count;
                        vertices.Count += 4;
                        SetupCubeVertexFace0(x, y, z + 1, 1f, 0, color, ref vertices.Array[count]);
                        SetupCubeVertexFace0(x + 1, y, z + 1, 1f, 1, color, ref vertices.Array[count + 1]);
                        SetupCubeVertexFace0(x + 1, y + 1, z + 1, 1f, 2, color, ref vertices.Array[count + 2]);
                        SetupCubeVertexFace0(x, y + 1, z + 1, 1f, 3, color, ref vertices.Array[count + 3]);
                        int count2 = indices.Count;
                        indices.Count += 6;
                        indices.Array[count2] = count;
                        indices.Array[count2 + 1] = (count + 2);
                        indices.Array[count2 + 2] = (count + 1);
                        indices.Array[count2 + 3] = (count + 2);
                        indices.Array[count2 + 4] = count;
                        indices.Array[count2 + 5] = (count + 3);
                    }

                    break;
                case 1:
                    chunkAtCell3 = Terrain.GetChunkAtCell(x + 1, z);
                    cellValueFast = chunkAtCell3.GetCellValueFast((x + 1) & 0xF, y, z & 0xF);
                    if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, face, value, cellValueFast))
                    {
                        DynamicArray<TerrainVertex> vertices2 = subsetsByFace[1].Vertices;
                        var indices2 = subsetsByFace[1].Indices;
                        int faceTextureSlot2 = block.GetFaceTextureSlot(1, value);
                        int count3 = vertices2.Count;
                        vertices2.Count += 4;
                        SetupCubeVertexFace1(x + 1, y, z, 1f, 1, color, ref vertices2.Array[count3]);
                        SetupCubeVertexFace1(x + 1, y + 1, z, 1f, 2, color, ref vertices2.Array[count3 + 1]);
                        SetupCubeVertexFace1(x + 1, y + 1, z + 1, 1f, 3, color, ref vertices2.Array[count3 + 2]);
                        SetupCubeVertexFace1(x + 1, y, z + 1, 1f, 0, color, ref vertices2.Array[count3 + 3]);
                        int count4 = indices2.Count;
                        indices2.Count += 6;
                        indices2.Array[count4] = count3;
                        indices2.Array[count4 + 1] = (count3 + 2);
                        indices2.Array[count4 + 2] = (count3 + 1);
                        indices2.Array[count4 + 3] = (count3 + 2);
                        indices2.Array[count4 + 4] = count3;
                        indices2.Array[count4 + 5] = (count3 + 3);
                    }

                    break;
                case 2:
                    chunkAtCell4 = Terrain.GetChunkAtCell(x, z - 1);
                    cellValueFast = chunkAtCell4.GetCellValueFast(x & 0xF, y, (z - 1) & 0xF);
                    if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, face, value, cellValueFast))
                    {
                        DynamicArray<TerrainVertex> vertices3 = subsetsByFace[2].Vertices;
                        var indices3 = subsetsByFace[2].Indices;
                        int faceTextureSlot3 = block.GetFaceTextureSlot(2, value);
                        int count5 = vertices3.Count;
                        vertices3.Count += 4;
                        SetupCubeVertexFace2(x, y, z, 1f, 1, color, ref vertices3.Array[count5]);
                        SetupCubeVertexFace2(x + 1, y, z, 1f, 0, color, ref vertices3.Array[count5 + 1]);
                        SetupCubeVertexFace2(x + 1, y + 1, z, 1f, 3, color, ref vertices3.Array[count5 + 2]);
                        SetupCubeVertexFace2(x, y + 1, z, 1f, 2, color, ref vertices3.Array[count5 + 3]);
                        int count6 = indices3.Count;
                        indices3.Count += 6;
                        indices3.Array[count6] = count5;
                        indices3.Array[count6 + 1] = (count5 + 1);
                        indices3.Array[count6 + 2] = (count5 + 2);
                        indices3.Array[count6 + 3] = (count5 + 2);
                        indices3.Array[count6 + 4] = (count5 + 3);
                        indices3.Array[count6 + 5] = count5;
                    }

                    break;
                case 3:
                    chunkAtCell5 = Terrain.GetChunkAtCell(x - 1, z);
                    cellValueFast = chunkAtCell5.GetCellValueFast((x - 1) & 0xF, y, z & 0xF);
                    if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, face, value, cellValueFast))
                    {
                        DynamicArray<TerrainVertex> vertices4 = subsetsByFace[3].Vertices;
                        var indices4 = subsetsByFace[3].Indices;
                        int faceTextureSlot4 = block.GetFaceTextureSlot(3, value);
                        int count7 = vertices4.Count;
                        vertices4.Count += 4;
                        SetupCubeVertexFace3(x, y, z, 1f, 0, color, ref vertices4.Array[count7]);
                        SetupCubeVertexFace3(x, y + 1, z, 1f, 3, color, ref vertices4.Array[count7 + 1]);
                        SetupCubeVertexFace3(x, y + 1, z + 1, 1f, 2, color, ref vertices4.Array[count7 + 2]);
                        SetupCubeVertexFace3(x, y, z + 1, 1f, 1, color, ref vertices4.Array[count7 + 3]);
                        int count8 = indices4.Count;
                        indices4.Count += 6;
                        indices4.Array[count8] = count7;
                        indices4.Array[count8 + 1] = (count7 + 1);
                        indices4.Array[count8 + 2] = (count7 + 2);
                        indices4.Array[count8 + 3] = (count7 + 2);
                        indices4.Array[count8 + 4] = (count7 + 3);
                        indices4.Array[count8 + 5] = count7;
                    }

                    break;
                case 4:
                    chunkAtCell = Terrain.GetChunkAtCell(x, z);
                    cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y + 1, z & 0xF);
                    if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, face, value, cellValueFast))
                    {
                        DynamicArray<TerrainVertex> vertices5 = subsetsByFace[4].Vertices;
                        var indices5 = subsetsByFace[4].Indices;
                        int faceTextureSlot5 = block.GetFaceTextureSlot(4, value);
                        int count9 = vertices5.Count;
                        vertices5.Count += 4;
                        SetupCubeVertexFace4(x, y + 1, z, 1f, 3, color, ref vertices5.Array[count9]);
                        SetupCubeVertexFace4(x + 1, y + 1, z, 1f, 2, color, ref vertices5.Array[count9 + 1]);
                        SetupCubeVertexFace4(x + 1, y + 1, z + 1, 1f, 1, color, ref vertices5.Array[count9 + 2]);
                        SetupCubeVertexFace4(x, y + 1, z + 1, 1f, 0, color, ref vertices5.Array[count9 + 3]);
                        int count10 = indices5.Count;
                        indices5.Count += 6;
                        indices5.Array[count10] = count9;
                        indices5.Array[count10 + 1] = (count9 + 1);
                        indices5.Array[count10 + 2] = (count9 + 2);
                        indices5.Array[count10 + 3] = (count9 + 2);
                        indices5.Array[count10 + 4] = (count9 + 3);
                        indices5.Array[count10 + 5] = count9;
                    }

                    break;
                case 5:
                    chunkAtCell = Terrain.GetChunkAtCell(x, z);
                    cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y - 1, z & 0xF);
                    if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, face, value, cellValueFast))
                    {
                        DynamicArray<TerrainVertex> vertices6 = subsetsByFace[5].Vertices;
                        var indices6 = subsetsByFace[5].Indices;
                        int faceTextureSlot6 = block.GetFaceTextureSlot(5, value);
                        int count11 = vertices6.Count;
                        vertices6.Count += 4;
                        SetupCubeVertexFace5(x, y, z, 1f, 0, color, ref vertices6.Array[count11]);
                        SetupCubeVertexFace5(x + 1, y, z, 1f, 1, color, ref vertices6.Array[count11 + 1]);
                        SetupCubeVertexFace5(x + 1, y, z + 1, 1f, 2, color, ref vertices6.Array[count11 + 2]);
                        SetupCubeVertexFace5(x, y, z + 1, 1f, 3, color, ref vertices6.Array[count11 + 3]);
                        int count12 = indices6.Count;
                        indices6.Count += 6;
                        indices6.Array[count12] = count11;
                        indices6.Array[count12 + 1] = (count11 + 2);
                        indices6.Array[count12 + 2] = (count11 + 1);
                        indices6.Array[count12 + 3] = (count11 + 2);
                        indices6.Array[count12 + 4] = count11;
                        indices6.Array[count12 + 5] = (count11 + 3);
                    }
                    break;
                default: break;

            }





        }
        public void GenerateCubeVertices(Block block, int value, int x, int y, int z, float height11, float height21, float height22, float height12, Color sideColor, Color topColor11, Color topColor21, Color topColor22, Color topColor12, int overrideTopTextureSlot, TerrainGeometrySubset[] subsetsByFace)
        {
            int blockIndex = block.BlockIndex;
            TerrainChunk chunkAtCell = Terrain.GetChunkAtCell(x, z);
            TerrainChunk chunkAtCell2 = Terrain.GetChunkAtCell(x, z + 1);
            TerrainChunk chunkAtCell3 = Terrain.GetChunkAtCell(x + 1, z);
            TerrainChunk chunkAtCell4 = Terrain.GetChunkAtCell(x, z - 1);
            TerrainChunk chunkAtCell5 = Terrain.GetChunkAtCell(x - 1, z);
            int cellValueFast = chunkAtCell2.GetCellValueFast(x & 0xF, y, (z + 1) & 0xF);
            if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, 0, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices = subsetsByFace[0].Vertices;
                var indices = subsetsByFace[0].Indices;
                int faceTextureSlot = block.GetFaceTextureSlot(0, value);
                int count = vertices.Count;
                vertices.Count += 4;
                SetupCubeVertexFace0(x, y, z + 1, 1f, 0, sideColor, ref vertices.Array[count]);
                SetupCubeVertexFace0(x + 1, y, z + 1, 1f, 1, sideColor, ref vertices.Array[count + 1]);
                SetupCubeVertexFace0(x + 1, y + 1, z + 1, height22, 2, sideColor, ref vertices.Array[count + 2]);
                SetupCubeVertexFace0(x, y + 1, z + 1, height12, 3, sideColor, ref vertices.Array[count + 3]);
                int count2 = indices.Count;
                indices.Count += 6;
                indices.Array[count2] = count;
                indices.Array[count2 + 1] = (count + 2);
                indices.Array[count2 + 2] = (count + 1);
                indices.Array[count2 + 3] = (count + 2);
                indices.Array[count2 + 4] = count;
                indices.Array[count2 + 5] = (count + 3);
            }

            cellValueFast = chunkAtCell3.GetCellValueFast((x + 1) & 0xF, y, z & 0xF);
            if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, 1, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices2 = subsetsByFace[1].Vertices;
                var indices2 = subsetsByFace[1].Indices;
                int faceTextureSlot2 = block.GetFaceTextureSlot(1, value);
                int count3 = vertices2.Count;
                vertices2.Count += 4;
                SetupCubeVertexFace1(x + 1, y, z, 1f, 1, sideColor, ref vertices2.Array[count3]);
                SetupCubeVertexFace1(x + 1, y + 1, z, height21, 2, sideColor, ref vertices2.Array[count3 + 1]);
                SetupCubeVertexFace1(x + 1, y + 1, z + 1, height22, 3, sideColor, ref vertices2.Array[count3 + 2]);
                SetupCubeVertexFace1(x + 1, y, z + 1, 1f, 0, sideColor, ref vertices2.Array[count3 + 3]);
                int count4 = indices2.Count;
                indices2.Count += 6;
                indices2.Array[count4] = count3;
                indices2.Array[count4 + 1] = (count3 + 2);
                indices2.Array[count4 + 2] = (count3 + 1);
                indices2.Array[count4 + 3] = (count3 + 2);
                indices2.Array[count4 + 4] = count3;
                indices2.Array[count4 + 5] = (count3 + 3);
            }

            cellValueFast = chunkAtCell4.GetCellValueFast(x & 0xF, y, (z - 1) & 0xF);
            if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, 2, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices3 = subsetsByFace[2].Vertices;
                var indices3 = subsetsByFace[2].Indices;
                int faceTextureSlot3 = block.GetFaceTextureSlot(2, value);
                int count5 = vertices3.Count;
                vertices3.Count += 4;
                SetupCubeVertexFace2(x, y, z, 1f, 1, sideColor, ref vertices3.Array[count5]);
                SetupCubeVertexFace2(x + 1, y, z, 1f, 0, sideColor, ref vertices3.Array[count5 + 1]);
                SetupCubeVertexFace2(x + 1, y + 1, z, height21, 3, sideColor, ref vertices3.Array[count5 + 2]);
                SetupCubeVertexFace2(x, y + 1, z, height11, 2, sideColor, ref vertices3.Array[count5 + 3]);
                int count6 = indices3.Count;
                indices3.Count += 6;
                indices3.Array[count6] = count5;
                indices3.Array[count6 + 1] = (count5 + 1);
                indices3.Array[count6 + 2] = (count5 + 2);
                indices3.Array[count6 + 3] = (count5 + 2);
                indices3.Array[count6 + 4] = (count5 + 3);
                indices3.Array[count6 + 5] = count5;
            }

            cellValueFast = chunkAtCell5.GetCellValueFast((x - 1) & 0xF, y, z & 0xF);
            if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, 3, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices4 = subsetsByFace[3].Vertices;
                var indices4 = subsetsByFace[3].Indices;
                int faceTextureSlot4 = block.GetFaceTextureSlot(3, value);
                int count7 = vertices4.Count;
                vertices4.Count += 4;
                SetupCubeVertexFace3(x, y, z, 1f, 0, sideColor, ref vertices4.Array[count7]);
                SetupCubeVertexFace3(x, y + 1, z, height11, 3, sideColor, ref vertices4.Array[count7 + 1]);
                SetupCubeVertexFace3(x, y + 1, z + 1, height12, 2, sideColor, ref vertices4.Array[count7 + 2]);
                SetupCubeVertexFace3(x, y, z + 1, 1f, 1, sideColor, ref vertices4.Array[count7 + 3]);
                int count8 = indices4.Count;
                indices4.Count += 6;
                indices4.Array[count8] = count7;
                indices4.Array[count8 + 1] = (count7 + 1);
                indices4.Array[count8 + 2] = (count7 + 2);
                indices4.Array[count8 + 3] = (count7 + 2);
                indices4.Array[count8 + 4] = (count7 + 3);
                indices4.Array[count8 + 5] = count7;
            }

            cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y + 1, z & 0xF);
            if ((Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, 4, value, cellValueFast)) || height11 < 1f || height12 < 1f || height21 < 1f || height22 < 1f)
            {
                DynamicArray<TerrainVertex> vertices5 = subsetsByFace[4].Vertices;
                var indices5 = subsetsByFace[4].Indices;
                int textureSlot = (overrideTopTextureSlot >= 0) ? overrideTopTextureSlot : block.GetFaceTextureSlot(4, value);
                int count9 = vertices5.Count;
                vertices5.Count += 4;
                SetupCubeVertexFace4(x, y + 1, z, height11, 3, topColor11, ref vertices5.Array[count9]);
                SetupCubeVertexFace4(x + 1, y + 1, z, height21, 2, topColor21, ref vertices5.Array[count9 + 1]);
                SetupCubeVertexFace4(x + 1, y + 1, z + 1, height22, 1, topColor22, ref vertices5.Array[count9 + 2]);
                SetupCubeVertexFace4(x, y + 1, z + 1, height12, 0, topColor12, ref vertices5.Array[count9 + 3]);
                int count10 = indices5.Count;
                indices5.Count += 6;
                indices5.Array[count10] = count9;
                indices5.Array[count10 + 1] = (count9 + 1);
                indices5.Array[count10 + 2] = (count9 + 2);
                indices5.Array[count10 + 3] = (count9 + 2);
                indices5.Array[count10 + 4] = (count9 + 3);
                indices5.Array[count10 + 5] = count9;
            }

            cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y - 1, z & 0xF);
            if (Terrain.ExtractContents(cellValueFast) != blockIndex && block.ShouldGenerateFace(SubsystemTerrain, 5, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices6 = subsetsByFace[5].Vertices;
                var indices6 = subsetsByFace[5].Indices;
                int faceTextureSlot5 = block.GetFaceTextureSlot(5, value);
                int count11 = vertices6.Count;
                vertices6.Count += 4;
                SetupCubeVertexFace5(x, y, z, 1f, 0, sideColor, ref vertices6.Array[count11]);
                SetupCubeVertexFace5(x + 1, y, z, 1f, 1, sideColor, ref vertices6.Array[count11 + 1]);
                SetupCubeVertexFace5(x + 1, y, z + 1, 1f, 2, sideColor, ref vertices6.Array[count11 + 2]);
                SetupCubeVertexFace5(x, y, z + 1, 1f, 3, sideColor, ref vertices6.Array[count11 + 3]);
                int count12 = indices6.Count;
                indices6.Count += 6;
                indices6.Array[count12] = count11;
                indices6.Array[count12 + 1] = (count11 + 2);
                indices6.Array[count12 + 2] = (count11 + 1);
                indices6.Array[count12 + 3] = (count11 + 2);
                indices6.Array[count12 + 4] = count11;
                indices6.Array[count12 + 5] = (count11 + 3);
            }
        }
        public void GenerateCubeVertices(Block block, int value, int x, int y, int z, int rotationX, int rotationY, int rotationZ, Color color, TerrainGeometrySubset[] subsetsByFace)
        {
            int blockIndex = block.BlockIndex;
            TerrainChunk chunkAtCell = Terrain.GetChunkAtCell(x, z);
            TerrainChunk chunkAtCell2 = Terrain.GetChunkAtCell(x, z + 1);
            TerrainChunk chunkAtCell3 = Terrain.GetChunkAtCell(x + 1, z);
            TerrainChunk chunkAtCell4 = Terrain.GetChunkAtCell(x, z - 1);
            TerrainChunk chunkAtCell5 = Terrain.GetChunkAtCell(x - 1, z);
            int cellValueFast = chunkAtCell2.GetCellValueFast(x & 0xF, y, (z + 1) & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 0, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices = subsetsByFace[0].Vertices;
                var indices = subsetsByFace[0].Indices;
                int faceTextureSlot = block.GetFaceTextureSlot(0, value);
                int count = vertices.Count;
                vertices.Count += 4;
                SetupCubeVertexFace0(x, y, z + 1, 1f, rotationZ, color, ref vertices.Array[count]);
                SetupCubeVertexFace0(x + 1, y, z + 1, 1f, 1 + rotationZ, color, ref vertices.Array[count + 1]);
                SetupCubeVertexFace0(x + 1, y + 1, z + 1, 1f, 2 + rotationZ, color, ref vertices.Array[count + 2]);
                SetupCubeVertexFace0(x, y + 1, z + 1, 1f, 3 + rotationZ, color, ref vertices.Array[count + 3]);
                int count2 = indices.Count;
                indices.Count += 6;
                indices.Array[count2] = count;
                indices.Array[count2 + 1] = (count + 2);
                indices.Array[count2 + 2] = (count + 1);
                indices.Array[count2 + 3] = (count + 2);
                indices.Array[count2 + 4] = count;
                indices.Array[count2 + 5] = (count + 3);
            }

            cellValueFast = chunkAtCell3.GetCellValueFast((x + 1) & 0xF, y, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 1, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices2 = subsetsByFace[1].Vertices;
                var indices2 = subsetsByFace[1].Indices;
                int faceTextureSlot2 = block.GetFaceTextureSlot(1, value);
                int count3 = vertices2.Count;
                vertices2.Count += 4;
                SetupCubeVertexFace1(x + 1, y, z, 1f, 1 + rotationX, color, ref vertices2.Array[count3]);
                SetupCubeVertexFace1(x + 1, y + 1, z, 1f, 2 + rotationX, color, ref vertices2.Array[count3 + 1]);
                SetupCubeVertexFace1(x + 1, y + 1, z + 1, 1f, 3 + rotationX, color, ref vertices2.Array[count3 + 2]);
                SetupCubeVertexFace1(x + 1, y, z + 1, 1f, rotationX, color, ref vertices2.Array[count3 + 3]);
                int count4 = indices2.Count;
                indices2.Count += 6;
                indices2.Array[count4] = count3;
                indices2.Array[count4 + 1] = (count3 + 2);
                indices2.Array[count4 + 2] = (count3 + 1);
                indices2.Array[count4 + 3] = (count3 + 2);
                indices2.Array[count4 + 4] = count3;
                indices2.Array[count4 + 5] = (count3 + 3);
            }

            cellValueFast = chunkAtCell4.GetCellValueFast(x & 0xF, y, (z - 1) & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 2, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices3 = subsetsByFace[2].Vertices;
                var indices3 = subsetsByFace[2].Indices;
                int faceTextureSlot3 = block.GetFaceTextureSlot(2, value);
                int count5 = vertices3.Count;
                vertices3.Count += 4;
                SetupCubeVertexFace2(x, y, z, 1f, 1 + rotationZ, color, ref vertices3.Array[count5]);
                SetupCubeVertexFace2(x + 1, y, z, 1f, rotationZ, color, ref vertices3.Array[count5 + 1]);
                SetupCubeVertexFace2(x + 1, y + 1, z, 1f, 3 + rotationZ, color, ref vertices3.Array[count5 + 2]);
                SetupCubeVertexFace2(x, y + 1, z, 1f, 2 + rotationZ, color, ref vertices3.Array[count5 + 3]);
                int count6 = indices3.Count;
                indices3.Count += 6;
                indices3.Array[count6] = count5;
                indices3.Array[count6 + 1] = (count5 + 1);
                indices3.Array[count6 + 2] = (count5 + 2);
                indices3.Array[count6 + 3] = (count5 + 2);
                indices3.Array[count6 + 4] = (count5 + 3);
                indices3.Array[count6 + 5] = count5;
            }

            cellValueFast = chunkAtCell5.GetCellValueFast((x - 1) & 0xF, y, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 3, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices4 = subsetsByFace[3].Vertices;
                var indices4 = subsetsByFace[3].Indices;
                int faceTextureSlot4 = block.GetFaceTextureSlot(3, value);
                int count7 = vertices4.Count;
                vertices4.Count += 4;
                SetupCubeVertexFace3(x, y, z, 1f, rotationX, color, ref vertices4.Array[count7]);
                SetupCubeVertexFace3(x, y + 1, z, 1f, 3 + rotationX, color, ref vertices4.Array[count7 + 1]);
                SetupCubeVertexFace3(x, y + 1, z + 1, 1f, 2 + rotationX, color, ref vertices4.Array[count7 + 2]);
                SetupCubeVertexFace3(x, y, z + 1, 1f, 1 + rotationX, color, ref vertices4.Array[count7 + 3]);
                int count8 = indices4.Count;
                indices4.Count += 6;
                indices4.Array[count8] = count7;
                indices4.Array[count8 + 1] = (count7 + 1);
                indices4.Array[count8 + 2] = (count7 + 2);
                indices4.Array[count8 + 3] = (count7 + 2);
                indices4.Array[count8 + 4] = (count7 + 3);
                indices4.Array[count8 + 5] = count7;
            }

            cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y + 1, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 4, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices5 = subsetsByFace[4].Vertices;
                var indices5 = subsetsByFace[4].Indices;
                int faceTextureSlot5 = block.GetFaceTextureSlot(4, value);
                int count9 = vertices5.Count;
                vertices5.Count += 4;
                SetupCubeVertexFace4(x, y + 1, z, 1f, 3 + rotationY, color, ref vertices5.Array[count9]);
                SetupCubeVertexFace4(x + 1, y + 1, z, 1f, 2 + rotationY, color, ref vertices5.Array[count9 + 1]);
                SetupCubeVertexFace4(x + 1, y + 1, z + 1, 1f, 1 + rotationY, color, ref vertices5.Array[count9 + 2]);
                SetupCubeVertexFace4(x, y + 1, z + 1, 1f, rotationY, color, ref vertices5.Array[count9 + 3]);
                int count10 = indices5.Count;
                indices5.Count += 6;
                indices5.Array[count10] = count9;
                indices5.Array[count10 + 1] = (count9 + 1);
                indices5.Array[count10 + 2] = (count9 + 2);
                indices5.Array[count10 + 3] = (count9 + 2);
                indices5.Array[count10 + 4] = (count9 + 3);
                indices5.Array[count10 + 5] = count9;
            }

            cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y - 1, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 5, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices6 = subsetsByFace[5].Vertices;
                var indices6 = subsetsByFace[5].Indices;
                int faceTextureSlot6 = block.GetFaceTextureSlot(5, value);
                int count11 = vertices6.Count;
                vertices6.Count += 4;
                SetupCubeVertexFace5(x, y, z, 1f, rotationY, color, ref vertices6.Array[count11]);
                SetupCubeVertexFace5(x + 1, y, z, 1f, 1 + rotationY, color, ref vertices6.Array[count11 + 1]);
                SetupCubeVertexFace5(x + 1, y, z + 1, 1f, 2 + rotationY, color, ref vertices6.Array[count11 + 2]);
                SetupCubeVertexFace5(x, y, z + 1, 1f, 3 + rotationY, color, ref vertices6.Array[count11 + 3]);
                int count12 = indices6.Count;
                indices6.Count += 6;
                indices6.Array[count12] = count11;
                indices6.Array[count12 + 1] = (count11 + 2);
                indices6.Array[count12 + 2] = (count11 + 1);
                indices6.Array[count12 + 3] = (count11 + 2);
                indices6.Array[count12 + 4] = count11;
                indices6.Array[count12 + 5] = (count11 + 3);
            }
        }
        public void GenerateCubeVerticesBase(Block block, int value, int x, int y, int z, int rotationX, int rotationY, int rotationZ, Color color, TerrainGeometrySubset[] subsetsByFace)
        {
            int blockIndex = block.BlockIndex;
            TerrainChunk chunkAtCell = Terrain.GetChunkAtCell(x, z);
            TerrainChunk chunkAtCell2 = Terrain.GetChunkAtCell(x, z + 1);
            TerrainChunk chunkAtCell3 = Terrain.GetChunkAtCell(x + 1, z);
            TerrainChunk chunkAtCell4 = Terrain.GetChunkAtCell(x, z - 1);
            TerrainChunk chunkAtCell5 = Terrain.GetChunkAtCell(x - 1, z);
            int cellValueFast = chunkAtCell2.GetCellValueFast(x & 0xF, y, (z + 1) & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 0, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices = subsetsByFace[0].Vertices;
                var indices = subsetsByFace[0].Indices;
                int faceTextureSlot = block.GetFaceTextureSlot(0, value);
                int count = vertices.Count;
                vertices.Count += 4;
                SetupCubeVertexFace0(x, y, z + 1, 1f, rotationZ, faceTextureSlot, color, ref vertices.Array[count]);
                SetupCubeVertexFace0(x + 1, y, z + 1, 1f, 1 + rotationZ, faceTextureSlot, color, ref vertices.Array[count + 1]);
                SetupCubeVertexFace0(x + 1, y + 1, z + 1, 1f, 2 + rotationZ, faceTextureSlot, color, ref vertices.Array[count + 2]);
                SetupCubeVertexFace0(x, y + 1, z + 1, 1f, 3 + rotationZ, faceTextureSlot, color, ref vertices.Array[count + 3]);
                int count2 = indices.Count;
                indices.Count += 6;
                indices.Array[count2] = count;
                indices.Array[count2 + 1] = (count + 2);
                indices.Array[count2 + 2] = (count + 1);
                indices.Array[count2 + 3] = (count + 2);
                indices.Array[count2 + 4] = count;
                indices.Array[count2 + 5] = (count + 3);
            }

            cellValueFast = chunkAtCell3.GetCellValueFast((x + 1) & 0xF, y, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 1, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices2 = subsetsByFace[1].Vertices;
                var indices2 = subsetsByFace[1].Indices;
                int faceTextureSlot2 = block.GetFaceTextureSlot(1, value);
                int count3 = vertices2.Count;
                vertices2.Count += 4;
                SetupCubeVertexFace1(x + 1, y, z, 1f, 1 + rotationX, faceTextureSlot2, color, ref vertices2.Array[count3]);
                SetupCubeVertexFace1(x + 1, y + 1, z, 1f, 2 + rotationX, faceTextureSlot2, color, ref vertices2.Array[count3 + 1]);
                SetupCubeVertexFace1(x + 1, y + 1, z + 1, 1f, 3 + rotationX, faceTextureSlot2, color, ref vertices2.Array[count3 + 2]);
                SetupCubeVertexFace1(x + 1, y, z + 1, 1f, rotationX, faceTextureSlot2, color, ref vertices2.Array[count3 + 3]);
                int count4 = indices2.Count;
                indices2.Count += 6;
                indices2.Array[count4] = count3;
                indices2.Array[count4 + 1] = (count3 + 2);
                indices2.Array[count4 + 2] = (count3 + 1);
                indices2.Array[count4 + 3] = (count3 + 2);
                indices2.Array[count4 + 4] = count3;
                indices2.Array[count4 + 5] = (count3 + 3);
            }

            cellValueFast = chunkAtCell4.GetCellValueFast(x & 0xF, y, (z - 1) & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 2, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices3 = subsetsByFace[2].Vertices;
                var indices3 = subsetsByFace[2].Indices;
                int faceTextureSlot3 = block.GetFaceTextureSlot(2, value);
                int count5 = vertices3.Count;
                vertices3.Count += 4;
                SetupCubeVertexFace2(x, y, z, 1f, 1 + rotationZ, faceTextureSlot3, color, ref vertices3.Array[count5]);
                SetupCubeVertexFace2(x + 1, y, z, 1f, rotationZ, faceTextureSlot3, color, ref vertices3.Array[count5 + 1]);
                SetupCubeVertexFace2(x + 1, y + 1, z, 1f, 3 + rotationZ, faceTextureSlot3, color, ref vertices3.Array[count5 + 2]);
                SetupCubeVertexFace2(x, y + 1, z, 1f, 2 + rotationZ, faceTextureSlot3, color, ref vertices3.Array[count5 + 3]);
                int count6 = indices3.Count;
                indices3.Count += 6;
                indices3.Array[count6] = count5;
                indices3.Array[count6 + 1] = (count5 + 1);
                indices3.Array[count6 + 2] = (count5 + 2);
                indices3.Array[count6 + 3] = (count5 + 2);
                indices3.Array[count6 + 4] = (count5 + 3);
                indices3.Array[count6 + 5] = count5;
            }

            cellValueFast = chunkAtCell5.GetCellValueFast((x - 1) & 0xF, y, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 3, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices4 = subsetsByFace[3].Vertices;
                var indices4 = subsetsByFace[3].Indices;
                int faceTextureSlot4 = block.GetFaceTextureSlot(3, value);
                int count7 = vertices4.Count;
                vertices4.Count += 4;
                SetupCubeVertexFace3(x, y, z, 1f, rotationX, faceTextureSlot4, color, ref vertices4.Array[count7]);
                SetupCubeVertexFace3(x, y + 1, z, 1f, 3 + rotationX, faceTextureSlot4, color, ref vertices4.Array[count7 + 1]);
                SetupCubeVertexFace3(x, y + 1, z + 1, 1f, 2 + rotationX, faceTextureSlot4, color, ref vertices4.Array[count7 + 2]);
                SetupCubeVertexFace3(x, y, z + 1, 1f, 1 + rotationX, faceTextureSlot4, color, ref vertices4.Array[count7 + 3]);
                int count8 = indices4.Count;
                indices4.Count += 6;
                indices4.Array[count8] = count7;
                indices4.Array[count8 + 1] = (count7 + 1);
                indices4.Array[count8 + 2] = (count7 + 2);
                indices4.Array[count8 + 3] = (count7 + 2);
                indices4.Array[count8 + 4] = (count7 + 3);
                indices4.Array[count8 + 5] = count7;
            }

            cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y + 1, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 4, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices5 = subsetsByFace[4].Vertices;
                var indices5 = subsetsByFace[4].Indices;
                int faceTextureSlot5 = block.GetFaceTextureSlot(4, value);
                int count9 = vertices5.Count;
                vertices5.Count += 4;
                SetupCubeVertexFace4(x, y + 1, z, 1f, 3 + rotationY, faceTextureSlot5, color, ref vertices5.Array[count9]);
                SetupCubeVertexFace4(x + 1, y + 1, z, 1f, 2 + rotationY, faceTextureSlot5, color, ref vertices5.Array[count9 + 1]);
                SetupCubeVertexFace4(x + 1, y + 1, z + 1, 1f, 1 + rotationY, faceTextureSlot5, color, ref vertices5.Array[count9 + 2]);
                SetupCubeVertexFace4(x, y + 1, z + 1, 1f, rotationY, faceTextureSlot5, color, ref vertices5.Array[count9 + 3]);
                int count10 = indices5.Count;
                indices5.Count += 6;
                indices5.Array[count10] = count9;
                indices5.Array[count10 + 1] = (count9 + 1);
                indices5.Array[count10 + 2] = (count9 + 2);
                indices5.Array[count10 + 3] = (count9 + 2);
                indices5.Array[count10 + 4] = (count9 + 3);
                indices5.Array[count10 + 5] = count9;
            }

            cellValueFast = chunkAtCell.GetCellValueFast(x & 0xF, y - 1, z & 0xF);
            if (block.ShouldGenerateFace(SubsystemTerrain, 5, value, cellValueFast))
            {
                DynamicArray<TerrainVertex> vertices6 = subsetsByFace[5].Vertices;
                var indices6 = subsetsByFace[5].Indices;
                int faceTextureSlot6 = block.GetFaceTextureSlot(5, value);
                int count11 = vertices6.Count;
                vertices6.Count += 4;
                SetupCubeVertexFace5(x, y, z, 1f, rotationY, faceTextureSlot6, color, ref vertices6.Array[count11]);
                SetupCubeVertexFace5(x + 1, y, z, 1f, 1 + rotationY, faceTextureSlot6, color, ref vertices6.Array[count11 + 1]);
                SetupCubeVertexFace5(x + 1, y, z + 1, 1f, 2 + rotationY, faceTextureSlot6, color, ref vertices6.Array[count11 + 2]);
                SetupCubeVertexFace5(x, y, z + 1, 1f, 3 + rotationY, faceTextureSlot6, color, ref vertices6.Array[count11 + 3]);
                int count12 = indices6.Count;
                indices6.Count += 6;
                indices6.Array[count12] = count11;
                indices6.Array[count12 + 1] = (count11 + 2);
                indices6.Array[count12 + 2] = (count11 + 1);
                indices6.Array[count12 + 3] = (count11 + 2);
                indices6.Array[count12 + 4] = count11;
                indices6.Array[count12 + 5] = (count11 + 3);
            }
        }




        public static void CalculateCubeVertexLight(int value, ref int light, ref int shadow)
        {
            int num = Terrain.ExtractContents(value);
            if (num == 0)
            {
                light = Math.Max(light, Terrain.ExtractLight(value));
                return;
            }

            light = Math.Max(light, Terrain.ExtractLight(value));
            shadow += BlocksManager.Blocks[num].GetShadowStrength(value);
        }

        public static int CombineLightAndShadow(int light, int shadow)
        {
            return MathUtils.Max(light - MathUtils.Max(shadow / 7, 0), 0);
        }

        public int CalculateVertexLightFace0(int x, int y, int z)
        {
            int light = 0;
            int shadow = 0;
            TerrainChunk chunkAtCell = Terrain.GetChunkAtCell(x - 1, z);
            int num = TerrainChunk.CalculateCellIndex((x - 1) & 0xF, y, z & 0xF);
            int cellValueFast = chunkAtCell.GetCellValueFast(num - 1);
            int cellValueFast2 = chunkAtCell.GetCellValueFast(num);
            CalculateCubeVertexLight(cellValueFast, ref light, ref shadow);
            CalculateCubeVertexLight(cellValueFast2, ref light, ref shadow);
            TerrainChunk chunkAtCell2 = Terrain.GetChunkAtCell(x, z);
            int num2 = TerrainChunk.CalculateCellIndex(x & 0xF, y, z & 0xF);
            int cellValueFast3 = chunkAtCell2.GetCellValueFast(num2 - 1);
            int cellValueFast4 = chunkAtCell2.GetCellValueFast(num2);
            CalculateCubeVertexLight(cellValueFast3, ref light, ref shadow);
            CalculateCubeVertexLight(cellValueFast4, ref light, ref shadow);
            return CombineLightAndShadow(light, shadow);
        }

        public int CalculateVertexLightFace1(int x, int y, int z)
        {
            int light = 0;
            int shadow = 0;
            TerrainChunk chunkAtCell = Terrain.GetChunkAtCell(x, z - 1);
            int num = TerrainChunk.CalculateCellIndex(x & 0xF, y, (z - 1) & 0xF);
            int cellValueFast = chunkAtCell.GetCellValueFast(num - 1);
            int cellValueFast2 = chunkAtCell.GetCellValueFast(num);
            CalculateCubeVertexLight(cellValueFast, ref light, ref shadow);
            CalculateCubeVertexLight(cellValueFast2, ref light, ref shadow);
            TerrainChunk chunkAtCell2 = Terrain.GetChunkAtCell(x, z);
            int num2 = TerrainChunk.CalculateCellIndex(x & 0xF, y, z & 0xF);
            int cellValueFast3 = chunkAtCell2.GetCellValueFast(num2 - 1);
            int cellValueFast4 = chunkAtCell2.GetCellValueFast(num2);
            CalculateCubeVertexLight(cellValueFast3, ref light, ref shadow);
            CalculateCubeVertexLight(cellValueFast4, ref light, ref shadow);
            return CombineLightAndShadow(light, shadow);
        }

        public int CalculateVertexLightFace2(int x, int y, int z)
        {
            int light = 0;
            int shadow = 0;
            TerrainChunk chunkAtCell = Terrain.GetChunkAtCell(x - 1, z - 1);
            int num = TerrainChunk.CalculateCellIndex((x - 1) & 0xF, y, (z - 1) & 0xF);
            int cellValueFast = chunkAtCell.GetCellValueFast(num - 1);
            int cellValueFast2 = chunkAtCell.GetCellValueFast(num);
            CalculateCubeVertexLight(cellValueFast, ref light, ref shadow);
            CalculateCubeVertexLight(cellValueFast2, ref light, ref shadow);
            TerrainChunk chunkAtCell2 = Terrain.GetChunkAtCell(x, z - 1);
            int num2 = TerrainChunk.CalculateCellIndex(x & 0xF, y, (z - 1) & 0xF);
            int cellValueFast3 = chunkAtCell2.GetCellValueFast(num2 - 1);
            int cellValueFast4 = chunkAtCell2.GetCellValueFast(num2);
            CalculateCubeVertexLight(cellValueFast3, ref light, ref shadow);
            CalculateCubeVertexLight(cellValueFast4, ref light, ref shadow);
            return CombineLightAndShadow(light, shadow);
        }

        public int CalculateVertexLightFace3(int x, int y, int z)
        {
            int light = 0;
            int shadow = 0;
            TerrainChunk chunkAtCell = Terrain.GetChunkAtCell(x - 1, z - 1);
            int num = TerrainChunk.CalculateCellIndex((x - 1) & 0xF, y, (z - 1) & 0xF);
            int cellValueFast = chunkAtCell.GetCellValueFast(num - 1);
            int cellValueFast2 = chunkAtCell.GetCellValueFast(num);
            CalculateCubeVertexLight(cellValueFast, ref light, ref shadow);
            CalculateCubeVertexLight(cellValueFast2, ref light, ref shadow);
            TerrainChunk chunkAtCell2 = Terrain.GetChunkAtCell(x - 1, z);
            int num2 = TerrainChunk.CalculateCellIndex((x - 1) & 0xF, y, z & 0xF);
            int cellValueFast3 = chunkAtCell2.GetCellValueFast(num2 - 1);
            int cellValueFast4 = chunkAtCell2.GetCellValueFast(num2);
            CalculateCubeVertexLight(cellValueFast3, ref light, ref shadow);
            CalculateCubeVertexLight(cellValueFast4, ref light, ref shadow);
            return CombineLightAndShadow(light, shadow);
        }

        public int CalculateVertexLightFace4(int x, int y, int z)
        {
            int light = 0;
            int shadow = 0;
            CalculateCubeVertexLight(Terrain.GetCellValueFastChunkExists(x - 1, y, z - 1), ref light, ref shadow);
            CalculateCubeVertexLight(Terrain.GetCellValueFastChunkExists(x, y, z - 1), ref light, ref shadow);
            CalculateCubeVertexLight(Terrain.GetCellValueFastChunkExists(x - 1, y, z), ref light, ref shadow);
            CalculateCubeVertexLight(Terrain.GetCellValueFastChunkExists(x, y, z), ref light, ref shadow);
            return CombineLightAndShadow(light, shadow);
        }

        public int CalculateVertexLightFace5(int x, int y, int z)
        {
            int light = 0;
            int shadow = 0;
            CalculateCubeVertexLight(Terrain.GetCellValueFastChunkExists(x - 1, y - 1, z - 1), ref light, ref shadow);
            CalculateCubeVertexLight(Terrain.GetCellValueFastChunkExists(x, y - 1, z - 1), ref light, ref shadow);
            CalculateCubeVertexLight(Terrain.GetCellValueFastChunkExists(x - 1, y - 1, z), ref light, ref shadow);
            CalculateCubeVertexLight(Terrain.GetCellValueFastChunkExists(x, y - 1, z), ref light, ref shadow);
            return CombineLightAndShadow(light, shadow);
        }

        public void SetupCubeVertexFace0(int x, int y, int z, float height, int corner, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace0(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 0, corner, ref vertex);
        }

        public void SetupCubeVertexFace1(int x, int y, int z, float height, int corner, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace1(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 1, corner, ref vertex);
        }

        public void SetupCubeVertexFace2(int x, int y, int z, float height, int corner, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace2(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 2, corner, ref vertex);
        }

        public void SetupCubeVertexFace3(int x, int y, int z, float height, int corner, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace3(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 3, corner, ref vertex);
        }

        public void SetupCubeVertexFace4(int x, int y, int z, float height, int corner, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace4(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 4, corner, ref vertex);
        }

        public void SetupCubeVertexFace5(int x, int y, int z, float height, int corner, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace5(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 5, corner, ref vertex);
        }

        public void SetupCubeVertexFace0(int x, int y, int z, float height, int corner, int textureSlot, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace0(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 0, textureSlot, corner, ref vertex);
        }

        public void SetupCubeVertexFace1(int x, int y, int z, float height, int corner, int textureSlot, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace1(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 1, textureSlot, corner, ref vertex);
        }

        public void SetupCubeVertexFace2(int x, int y, int z, float height, int corner, int textureSlot, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace2(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 2, textureSlot, corner, ref vertex);
        }

        public void SetupCubeVertexFace3(int x, int y, int z, float height, int corner, int textureSlot, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace3(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 3, textureSlot, corner, ref vertex);
        }

        public void SetupCubeVertexFace4(int x, int y, int z, float height, int corner, int textureSlot, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace4(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 4, textureSlot, corner, ref vertex);
        }

        public void SetupCubeVertexFace5(int x, int y, int z, float height, int corner, int textureSlot, Color color, ref TerrainVertex vertex)
        {
            float y2 = (float)y + height - 1f;
            int light = CalculateVertexLightFace5(x, y, z);
            SetupCornerVertex(x, y2, z, color, light, 5, textureSlot, corner, ref vertex);
        }


        public static Vector3 GetRandomWireOffset(Vector3 position, Vector3 normal)
        {
            int hashCode = Vector3.Round(2f * position).GetHashCode();
            Vector3 result = default(Vector3);
            result.X = ((normal.X == 0f) ? ((float)(double)(MathUtils.Hash((uint)hashCode) % 255u) / 255f - 0.5f) : 0f);
            result.Y = ((normal.Y == 0f) ? ((float)(double)(MathUtils.Hash((uint)(hashCode + 1)) % 255u) / 255f - 0.5f) : 0f);
            result.Z = ((normal.Z == 0f) ? ((float)(double)(MathUtils.Hash((uint)(hashCode + 2)) % 255u) / 255f - 0.5f) : 0f);
            return result;
        }

        public void CalculateCornerLights(int x, int y, int z)
        {
            if (!(m_cornerLightsPosition == new Point3(x, y, z)))
            {
                m_cornerLightsPosition = new Point3(x, y, z);
                m_cornerLightsByFace[0].L000 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace0(x, y, z)];
                m_cornerLightsByFace[0].L001 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace0(x, y, z + 1)];
                m_cornerLightsByFace[0].L010 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace0(x, y + 1, z)];
                m_cornerLightsByFace[0].L011 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace0(x, y + 1, z + 1)];
                m_cornerLightsByFace[0].L100 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace0(x + 1, y, z)];
                m_cornerLightsByFace[0].L101 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace0(x + 1, y, z + 1)];
                m_cornerLightsByFace[0].L110 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace0(x + 1, y + 1, z)];
                m_cornerLightsByFace[0].L111 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace0(x + 1, y + 1, z + 1)];
                m_cornerLightsByFace[1].L000 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace1(x, y, z)];
                m_cornerLightsByFace[1].L001 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace1(x, y, z + 1)];
                m_cornerLightsByFace[1].L010 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace1(x, y + 1, z)];
                m_cornerLightsByFace[1].L011 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace1(x, y + 1, z + 1)];
                m_cornerLightsByFace[1].L100 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace1(x + 1, y, z)];
                m_cornerLightsByFace[1].L101 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace1(x + 1, y, z + 1)];
                m_cornerLightsByFace[1].L110 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace1(x + 1, y + 1, z)];
                m_cornerLightsByFace[1].L111 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace1(x + 1, y + 1, z + 1)];
                m_cornerLightsByFace[2].L000 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace2(x, y, z)];
                m_cornerLightsByFace[2].L001 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace2(x, y, z + 1)];
                m_cornerLightsByFace[2].L010 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace2(x, y + 1, z)];
                m_cornerLightsByFace[2].L011 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace2(x, y + 1, z + 1)];
                m_cornerLightsByFace[2].L100 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace2(x + 1, y, z)];
                m_cornerLightsByFace[2].L101 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace2(x + 1, y, z + 1)];
                m_cornerLightsByFace[2].L110 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace2(x + 1, y + 1, z)];
                m_cornerLightsByFace[2].L111 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace2(x + 1, y + 1, z + 1)];
                m_cornerLightsByFace[3].L000 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace3(x, y, z)];
                m_cornerLightsByFace[3].L001 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace3(x, y, z + 1)];
                m_cornerLightsByFace[3].L010 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace3(x, y + 1, z)];
                m_cornerLightsByFace[3].L011 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace3(x, y + 1, z + 1)];
                m_cornerLightsByFace[3].L100 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace3(x + 1, y, z)];
                m_cornerLightsByFace[3].L101 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace3(x + 1, y, z + 1)];
                m_cornerLightsByFace[3].L110 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace3(x + 1, y + 1, z)];
                m_cornerLightsByFace[3].L111 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace3(x + 1, y + 1, z + 1)];
                m_cornerLightsByFace[4].L000 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace4(x, y, z)];
                m_cornerLightsByFace[4].L001 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace4(x, y, z + 1)];
                m_cornerLightsByFace[4].L010 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace4(x, y + 1, z)];
                m_cornerLightsByFace[4].L011 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace4(x, y + 1, z + 1)];
                m_cornerLightsByFace[4].L100 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace4(x + 1, y, z)];
                m_cornerLightsByFace[4].L101 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace4(x + 1, y, z + 1)];
                m_cornerLightsByFace[4].L110 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace4(x + 1, y + 1, z)];
                m_cornerLightsByFace[4].L111 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace4(x + 1, y + 1, z + 1)];
                m_cornerLightsByFace[5].L000 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace5(x, y, z)];
                m_cornerLightsByFace[5].L001 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace5(x, y, z + 1)];
                m_cornerLightsByFace[5].L010 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace5(x, y + 1, z)];
                m_cornerLightsByFace[5].L011 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace5(x, y + 1, z + 1)];
                m_cornerLightsByFace[5].L100 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace5(x + 1, y, z)];
                m_cornerLightsByFace[5].L101 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace5(x + 1, y, z + 1)];
                m_cornerLightsByFace[5].L110 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace5(x + 1, y + 1, z)];
                m_cornerLightsByFace[5].L111 = LightingManager.LightIntensityByLightValue[CalculateVertexLightFace5(x + 1, y + 1, z + 1)];
            }
        }

        public float InterpolateCornerLights(int face, Vector3 position)
        {
            float x = position.X;
            float y = position.Y;
            float z = position.Z;
            float num = 1f - x;
            float num2 = 1f - y;
            float num3 = 1f - z;
            return m_cornerLightsByFace[face].L000 * num * num2 * num3 + m_cornerLightsByFace[face].L001 * num * num2 * z + m_cornerLightsByFace[face].L010 * num * y * num3 + m_cornerLightsByFace[face].L011 * num * y * z + m_cornerLightsByFace[face].L100 * x * num2 * num3 + m_cornerLightsByFace[face].L101 * x * num2 * z + m_cornerLightsByFace[face].L110 * x * y * num3 + m_cornerLightsByFace[face].L111 * x * y * z;
        }
    }
}